import { Injectable } from '@nestjs/common';
import { CreateRuleInfoDto } from './dto/create-rule-info.dto';
import { UpdateRuleInfoDto } from './dto/update-rule-info.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { RuleInfo } from './entities/rule-info.entity';
import { Repository } from 'typeorm';
import { buildQuery } from '../../../util/common-fn';
import { PageQuery } from '../../../util/entity/page.entity';
import { Role } from '../role/entities/role.entity';

@Injectable()
export class RuleInfoService {
  constructor(
    @InjectRepository(RuleInfo)
    private ruleRepository: Repository<RuleInfo>,
  ) {}
  async create(createRuleInfoDto: CreateRuleInfoDto) {
    return this.ruleRepository.save(createRuleInfoDto);
  }

  async findAll(pageQuery: PageQuery<Role>) {
    return this.ruleRepository.findAndCount(buildQuery(pageQuery));
  }

  async findOne(id: number) {
    return this.ruleRepository.findOneByOrFail({ id });
  }

  async update(id: number, updateRuleInfoDto: UpdateRuleInfoDto) {
    return this.ruleRepository.update({ id }, updateRuleInfoDto);
  }

  async remove(id: number) {
    await this.findOne(id);
    return this.ruleRepository.softDelete({ id });
  }
}
